﻿Public Class Form1
    Dim ocon As SqlClient.SqlConnection = Nothing

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click

        CleanConnection()

        DoConnection()

    End Sub

    Private Sub DoConnection()

        Try
            Dim strCon As String = _
                "Data source={0};Initial catalog={1};"

            strCon = String.Format(strCon, Me.txtServer.Text, Me.txtDataBase.Text)

            If chkIntegrated.Checked Then
                strCon = strCon & "Integrated Security=true;"
            Else
                strCon = strCon & "User Id={0};Pwd={1};"
                strCon = String.Format(strCon, Me.txtUser.Text, Me.txtPassword.Text)
            End If

            strCon = strCon & "Application Name=MONGA;"

            ocon = New SqlClient.SqlConnection(strCon)

            If Not ocon.State = ConnectionState.Open Then ocon.Open()

            Me.lblConnectionStatus.Text = "Conectted to database."

        Catch ex As Exception
            Me.lblConnectionStatus.Text = "Connection fails."

            CleanConnection()
        End Try
    End Sub

    Private Sub cmdFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFilter.Click

        CleanConnection()
        DoConnection()

        Dim ocom As SqlClient.SqlCommand = Nothing
        Dim dtResults As DataTable = Nothing

        Try

            ocom = New SqlClient.SqlCommand("Select Id, DirName, LeafName, Size, Extension From AllDocs Where ", ocon)

            ocom.CommandType = CommandType.Text
            ocom.CommandText += " LeafName like '%" & Me.txtNameFilter.Text & "%' and Extension in ('" & Me.txtExtensions.Text.Replace(",", "','") & "') Order by LeafName Asc"

            dtResults = New DataTable
            dtResults.Load(ocom.ExecuteReader())

            Me.dgvResults.DataSource = dtResults

            If Not IsNothing(dtResults) Then dtResults.Dispose()
            dtResults = Nothing
        Catch ex As Exception

        Finally
            If Not IsNothing(ocom) Then ocom.Dispose()
            ocom = Nothing
            If Not IsNothing(dtResults) Then dtResults.Dispose()
            dtResults = Nothing

            CleanConnection()
            GC.Collect()
        End Try
    End Sub
    Private Sub CleanConnection()

        If Not IsNothing(ocon) Then
            If Not ocon.State = ConnectionState.Closed Then ocon.Close()
            ocon.Dispose()
        End If
        ocon = Nothing

        Me.lblConnectionStatus.Text = "Connection Closed"
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        CleanConnection()
    End Sub

    Private Sub cmdExports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExports.Click
        CleanConnection()
        DoConnection()
        Dim ocom As SqlClient.SqlCommand = Nothing
        Dim owrt As IO.FileStream = Nothing

        Try
            Dim ores As DialogResult = Me.fbdSelectDestination.ShowDialog

            If ores = Windows.Forms.DialogResult.OK Or ores = Windows.Forms.DialogResult.Yes Then
                For Each dgvr As DataGridViewRow In Me.dgvResults.SelectedRows

                    If IsNothing(dgvr.Cells(0).Value) Then Continue For

                    If Not IsNothing(ocom) Then ocom.Dispose()
                    ocom = Nothing

                    ocom = New SqlClient.SqlCommand("Select adc.Content, adc.Size, ad.Extension, ad.LeafName From AllDocStreams adc inner join AllDocs ad on ad.Id = adc.Id Where adc.Id='" & dgvr.Cells(0).Value.ToString() & "'", ocon)
                    ocom.CommandType = CommandType.Text
                    Dim odr As SqlClient.SqlDataReader = Nothing
                    odr = ocom.ExecuteReader()

                    While odr.Read

                        If IO.File.Exists(Me.fbdSelectDestination.SelectedPath & "\" & odr("LeafName").ToString()) Then
                            IO.File.Delete(Me.fbdSelectDestination.SelectedPath & "\" & odr("LeafName").ToString())
                        End If

                        If Not IsNothing(owrt) Then owrt.Dispose()
                        owrt = Nothing
                        owrt = New IO.FileStream( _
                            Me.fbdSelectDestination.SelectedPath & "\" & odr("LeafName").ToString(), IO.FileMode.CreateNew)
                        owrt.Write(odr("Content"), 0, _
                                   Convert.ToInt32(odr("Size").ToString()) - 1)
                        owrt.Flush()
                        owrt.Close()

                    End While

                    odr.Close()
                    odr = Nothing

                Next
            End If

            Windows.Forms.MessageBox.Show("Restoration process sucessful!!!", "MONGA WSS 3 Files Restorer", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

        Catch ex As Exception
        Finally

            If Not IsNothing(ocom) Then ocom.Dispose()
            ocom = Nothing
            CleanConnection()

        End Try
    End Sub
End Class
